package main

import (
	"fmt"
	"sort"
)

func merge(ins [][]int) [][]int {

	if len(ins) == 0 {
		return [][]int{}
	}

	sort.Slice(ins, func(i, j int) bool {
		return ins[i][0] < ins[j][0]
	})

	var merged [][]int

	curr := ins[0]

	merged = append(merged, curr)

	for _, in := range ins {
		currEnd := curr[1]
		nextStart, nextEnd := in[0], in[1]
		if currEnd >= nextStart {
			curr[1] = max(currEnd, nextEnd)
		} else {
			curr = in
			merged = append(merged, curr)
		}
	}
	return merged
}

func main() {
	intervals1 := [][]int{{1, 3}, {2, 6}, {8, 10}, {15, 18}}
	fmt.Println("输出", merge(intervals1))

	intervals2 := [][]int{{1, 4}, {4, 5}}
	fmt.Println("输出", merge(intervals2))

}
